﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HRZoneTrainer
{
    public partial class HRZone : Form
    {
        public enum ZoneListViewColumns
        {
            Name = 0,
            LowerPercent = 1,
            UpperPercent = 2,
            LowerHR = 3,
            UpperHR = 4,
            Accomplishment = 5
        }

        public HRZone(int hRThreshold, int hRMaximum, string currentZoneSelection)
        {
            m_zoneCalculation = new ZoneCalculation(hRThreshold, hRMaximum, currentZoneSelection);
            InitializeComponent();
        }

        private void HRZone_Load(object sender, EventArgs e)
        {
            foreach ( ZonesZone thisZone in m_zoneCalculation.Zones.Items )
            {
                comboBoxZoneCalculationMethod.Items.Add(thisZone.Name);
            }

            comboBoxZoneCalculationMethod.SelectedIndex = m_zoneCalculation.CurrentZoneSelectionIndex;

            textBoxHRThreshold.Text = m_zoneCalculation.HRThreshold.ToString();
            textBoxHRMaximum.Text = m_zoneCalculation.HRMaximum.ToString();

            UpdateZoneCalculations();
        }

        private void textBoxHR_TextChanged(object sender, EventArgs e)
        {
            UpdateZoneCalculations();
        }

        private void UpdateZoneCalculations()
        {
            listViewZones.BeginUpdate();

            listViewZones.Items.Clear();

            foreach ( ZonesZoneTrainingZonesTrainingZone thisTrainingZone in m_zoneCalculation.Zones.Items[comboBoxZoneCalculationMethod.SelectedIndex].TrainingZones )
            {
                ListViewItem thisItem = new ListViewItem(ToListViewItems(thisTrainingZone));
                listViewZones.Items.Add(thisItem);
            }

            listViewZones.EndUpdate();
        }

        private string[] ToListViewItems(ZonesZoneTrainingZonesTrainingZone thisTrainingZone)
        {
            string[] theseItems = new string[6];

            theseItems[(int) ZoneListViewColumns.Name] = thisTrainingZone.Name;
            theseItems[(int) ZoneListViewColumns.LowerPercent] = thisTrainingZone.Percentage.Lower.ToString();
            theseItems[(int) ZoneListViewColumns.UpperPercent] = thisTrainingZone.Percentage.Upper.ToString();
            theseItems[(int) ZoneListViewColumns.Accomplishment] = thisTrainingZone.Accomplishment;

            theseItems[(int) ZoneListViewColumns.LowerHR] = m_zoneCalculation.CalculateLower(thisTrainingZone.Name).ToString("0");
            theseItems[(int) ZoneListViewColumns.UpperHR] = m_zoneCalculation.CalculateUpper(thisTrainingZone.Name).ToString("0");

            return ( theseItems );
        }

        private void comboBoxZoneCalculationMethod_SelectedIndexChanged(object sender, EventArgs e)
        {
            m_zoneCalculation.CurrentZoneSelection = comboBoxZoneCalculationMethod.SelectedItem.ToString();
            UpdateZoneCalculations();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                m_zoneCalculation.HRThreshold = Int32.Parse(textBoxHRThreshold.Text);
                m_zoneCalculation.HRMaximum = Int32.Parse(textBoxHRMaximum.Text);
                m_zoneCalculation.CurrentZoneSelection = comboBoxZoneCalculationMethod.SelectedItem.ToString();
            }
            catch ( Exception )
            {

            }
        }

        public ZoneCalculation m_zoneCalculation = null;
    }
}
